home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 2 / Atari Mega Archive CD - Volume 2.iso / minix / up1510b.tgz / up1510b / src / commands / printroot.c < prev    next >
C/C++ Source or Header  |  1990-07-19  |  2KB  |  71 lines

  1. /* printroot - print root device on stdout    Author: Bruce Evans */
  2.  
  3. /* This program figures out what the root device is by doing a stat on it, and
  4.  * then searching /dev until it finds an entry with the same device number.
  5.  * A typical use (probably the only use) is in /etc/rc for initializing
  6.  * /etc/mtab, as follows:
  7.  *
  8.  *    /usr/bin/printroot >/etc/mtab
  9.  *
  10.  *  9 Dec     1989 - clean up for 1.5 - full prototypes (BDE)
  11.  * 15 October 1989 - avoid ACK cc bugs (BDE):
  12.  *           - sizeof "foo" is 2 (from wrong type char *) instead of 4
  13.  *           - char foo[10] = "bar"; allocates 4 bytes instead of 10
  14.  *  1 October 1989 - Minor changes by Andy Tanenbaum
  15.  */
  16.  
  17. #include <sys/types.h>
  18. #include <sys/dir.h>
  19. #include <sys/stat.h>
  20. #include <fcntl.h>
  21. #include <limits.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <unistd.h>
  25.  
  26. static char DEV_PATH[] = "/dev/";    /* #define would step on sizeof bug */
  27. static char MESSAGE[] =    " is root device\n";    /* ditto */
  28. #define UNKNOWN_DEV    "/dev/unknown"
  29. #define ROOT        "/"
  30.  
  31. #ifdef __STDC__
  32. static void done(char *name, int status);
  33. #else
  34. static void done();
  35. #endif
  36.  
  37. int main(argc, argv)
  38. int argc;
  39. char **argv;
  40. {
  41.   struct direct dir;
  42.   int fd;
  43.   struct stat filestat, rootstat;
  44.   static char namebuf[sizeof DEV_PATH + NAME_MAX];
  45.  
  46.   if (stat(ROOT, &rootstat) == 0 && (fd = open(DEV_PATH, O_RDONLY)) >= 0) {
  47.     while (read(fd, (char *) &dir, sizeof dir) == sizeof dir) {
  48.         if (dir.d_ino == 0) continue;
  49.         strcpy(namebuf, DEV_PATH);
  50.  
  51.         /* If next does not null-terminate, last in buf does it. */
  52.         strncat(namebuf, dir.d_name, NAME_MAX);
  53.         if (stat(namebuf, &filestat) != 0) continue;
  54.         if ((filestat.st_mode & S_IFMT) != S_IFBLK) continue;
  55.         if (filestat.st_rdev != rootstat.st_dev) continue;
  56.         done(namebuf, 0);
  57.     }
  58.   }
  59.   done(UNKNOWN_DEV, 1);
  60.   return 0;            /* not reached */
  61. }
  62.  
  63. static void done(name, status)
  64. char *name;
  65. int status;
  66. {
  67.   write(1, name, strlen(name));
  68.   write(1, MESSAGE, sizeof MESSAGE - 1);
  69.   exit(status);
  70. }
  71.